<?php

namespace app\stationpc\controller;

use app\model\StationBoxOrder as ModelStationBoxOrder;
use app\model\StationBoxOrderLog as ModelStationBoxOrderLog;
use app\model\DriverOrder as ModelDriverOrder;
use app\model\DriverInfo as ModelDriverInfo;
use app\model\StationInfo as ModelStationInfo;
use app\model\Express as ModelExpress;
use app\model\StationOrderLog as ModelStationOrderLog;


class PackingManagement extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 装箱列表 \n
     * URI : /stationpc/packing.management.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  boxOrderNumber    | string | 选填 | 装箱编号
     *  receiveStationName| string | 选填 | 目的地
     *  operator          | string | 选填 | 操作人员
     *  order             | string | 必填 | 必填倒序（order = ‘desc’）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     *  boxOrderId     | int    | ID
     *  boxOrderNumber | string | 装箱编号
     *  receiveStationName      | string | 目的地
     *  operator       | int    | 操作人员
     *
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function packingManagementList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $str_box_order_number = $this->_getContentParams('boxOrderNumber');     //装箱编号
        $str_receive_station = $this->_getContentParams('receiveStation');     //目的地
        $str_operator = $this->_getContentParams('operator');     //操作人员
        $str_order = $this->_getContentParams('order');     //排序

        $arr_where = $this->packingManagementListWhere($str_box_order_number, $str_receive_station, $str_operator);
        $str_order = $this->listOrder('gmt_create', $str_order);

        $int_total_rows = ModelStationBoxOrderLog::getInstance()->toCount($arr_where);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'id AS boxOrderId,box_order_number AS boxOrderNumber,operator';
        $arr_list = ModelStationBoxOrderLog::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $str_order);
        $arr_list = $this->packingManagementListModify($arr_list);
        _returnData(['totalPages' => $int_total_pages, 'data' => $arr_list]);
    }

    /**
     * 装箱列表 -> 筛选
     * @param $str_box_order_number
     * @param $str_receive_station
     * @param $str_operator
     * @return array
     */
    private function packingManagementListWhere($str_box_order_number, $str_receive_station, $str_operator){
        $arr_where = [
            'state' => 10,
        ];
        if($str_box_order_number != ''){
            $arr_where['box_order_number'] = ['like', "%$str_box_order_number%"];
        }
        if($str_operator != ''){
            $arr_where['operator'] = ['like', "%$str_operator%"];
        }
        if($str_receive_station != ''){
            $arr_info_id = ModelStationBoxOrder::getInstance()->toColumn(['receive_station_name' => ['like', "%$str_receive_station%"]], 'box_order_number');
            $arr_where['box_order_number'] = ['in', implode(",", $arr_info_id)];
        }
        return $arr_where;
    }

    /**
     * 装箱列表 -> 操作人名称
     * @param $arr_list
     * @return mixed
     */
    private function packingManagementListModify($arr_list){
        foreach ($arr_list as &$value) {
            $value['receiveStationName'] = (string)ModelStationBoxOrder::getInstance()->toValue(['box_order_number' => $value['boxOrderNumber']], 'receive_station_name');
        }
        return $arr_list;
    }

    /**
     * 拆箱列表 \n
     * URI : /stationpc/unpacking.management.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  boxOrderNumber    | string | 选填 | 装箱编号
     *  sendStationName   | string | 选填 | 起始地
     *  operator          | string | 选填 | 操作人员
     *  order             | string | 必填 | 必填倒序（order = ‘desc’）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     *  boxOrderId     | int    | ID
     *  boxOrderNumber | string | 装箱编号
     *  sendStationName| string | 起始地
     *  operator       | int    | 操作人员
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function unpackingManagementList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $str_box_order_number = $this->_getContentParams('boxOrderNumber');     //装箱编号
        $str_send_station_name = $this->_getContentParams('sendStationName');     //起始地
        $str_operator = $this->_getContentParams('operator');     //操作人员
        $str_order = $this->_getContentParams('order');     //排序

        $arr_where = $this->unpackingManagementListWhere($str_box_order_number, $str_send_station_name, $str_operator);
        $str_order = $this->listOrder('gmt_create', $str_order);

        $int_total_rows = ModelStationBoxOrderLog::getInstance()->toCount($arr_where);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'id AS boxOrderId,box_order_number AS boxOrderNumber,operator';
        $arr_list = ModelStationBoxOrderLog::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $str_order);
        $arr_list = $this->unpackingManagementListModify($arr_list);
        _returnData(['totalPages' => $int_total_pages, 'data' => $arr_list]);
    }

    /**
     * 拆箱列表 -> 筛选
     * @param $str_box_order_number
     * @param $str_send_station_name
     * @param $str_operator
     * @return array
     */
    private function unpackingManagementListWhere($str_box_order_number, $str_send_station_name, $str_operator){
        $arr_where = [
            'state' => 15,
        ];
        if($str_box_order_number != ''){
            $arr_where['box_order_number'] = ['like', "%$str_box_order_number%"];
        }
        if($str_operator != ''){
            $arr_where['operator'] = ['like', "%$str_operator%"];
        }
        if($str_send_station_name != ''){
            $arr_info_id = ModelStationBoxOrder::getInstance()->toColumn(['receive_station_name' => ['like', "%$str_send_station_name%"]], 'box_order_number');
            $arr_where['box_order_number'] = ['in', implode(",", $arr_info_id)];
        }
        return $arr_where;
    }

    /**
     * 拆箱列表 -> 操作人名称
     * @param $arr_list
     * @return mixed
     */
    private function unpackingManagementListModify($arr_list){
        foreach ($arr_list as &$value) {
            $value['sendStationName'] = (string)ModelStationBoxOrder::getInstance()->toValue(['box_order_number' => $value['boxOrderNumber']], 'send_station_name');
        }
        return $arr_list;
    }

    /**
     * 中转列表 \n
     * URI : /stationpc/transit.management.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  boxOrderNumber    | string | 选填 | 装箱编号
     *  state             | string | 选填 | 装箱状态
     *  order             | string | 必填 | 必填倒序（order = ‘desc’）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     *  boxOrderId     | int    | ID
     *  boxOrderNumber | string | 装箱编号
     *  state          | int    | 装箱状态 10：装箱；20:待中转，（10的时候预约亮起）
     *  carNumber      | string | 车牌
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function transitManagementList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $str_box_order_number = $this->_getContentParams('boxOrderNumber');     //装箱编号
        $str_state = $this->_getContentParams('state');     //装箱状态
        $str_order = $this->_getContentParams('order');     //排序

        $arr_where = $this->transitManagementListWhere($str_box_order_number, $str_state);
        $str_order = $this->listOrder('gmt_create', $str_order);

        $int_total_rows = ModelStationBoxOrderLog::getInstance()->toCount($arr_where);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'id AS boxOrderLogId,box_order_number AS boxOrderNumber,state';
        $arr_list = ModelStationBoxOrderLog::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $str_order);
        $arr_list = $this->transitManagementListModify($arr_list);
        _returnData(['totalPages' => $int_total_pages, 'data' => $arr_list]);
    }

    /**
     * 中转列表 -> 筛选
     * @param $str_box_order_number
     * @param $str_state
     * @return array
     */
    private function transitManagementListWhere($str_box_order_number, $str_state){
        $arr_where = [
            'state' => ['in', "10,20"],
        ];
        if($str_box_order_number != ''){
            $arr_where['box_order_number'] = ['like', "%$str_box_order_number%"];
        }
        if($str_state != ''){
            $arr_where['state'] = $str_state;
        }
        return $arr_where;
    }

    /**
     * 中转列表 -> 车牌
     * @param $arr_list
     * @return mixed
     */
    private function transitManagementListModify($arr_list){
        foreach ($arr_list as &$value) {
            $str_info_id = ModelStationBoxOrder::getInstance()->toValue(['box_order_number' => $value['boxOrderNumber']], 'id');
            $value['carNumber'] = (string)ModelDriverOrder::getInstance()->toValue(['box_id' => $str_info_id], 'car_number');
        }
        return $arr_list;
    }

    /**
     * 中转列表->预约 \n
     * URI : /stationpc/transit.management.list.appointment
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  boxOrderNumber    | string | 必填 | 装箱编号
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function transitManagementListAppointment(){
        $str_box_order_number = $this->_getContentParams('boxOrderNumber'); // 装箱编号
        _toCompare(empty($str_box_order_number), '===', true, 30009);
        ModelStationBoxOrder::getInstance()->toUpdate(['box_order_number' => $str_box_order_number], ['state' => 20]);
        _returnData();
    }

    /**
     * 装箱管理详情 \n
     * URI : /stationpc/packing.list.details
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  stationOrderId    | int | 必填 | 站点订单ID
     *
     * @return :
     *  name            | type   | description
     * -----------------|--------|-------------
     *  stationBoxOrderId   | int    | ID
     *  boxOrderNumber      | float  | 装箱编号
     *  operator            | string | 装箱操作人员
     *  gmtCreate           | string | 装箱时间
     *  -------             | string | 预计到达时间
     *  state               | int    | 状态信息 10：装箱；15:拆箱；20:待中转；
     *  carNumber           | string | 配送车辆
     *  boxContent          | int    | 内含包裹(件)
     *  totalWeight         | float  | 整箱质量:KG
     *  volume              | float  | 整箱体积:M
     *  operator            | string | 始发站点负责人
     *  pickCode            | string | 司机取件码
     *  expressName         | string | 所属快递
     *  expectedArrivalTime | string | 预计到达时间
     *  actualArrivalTime   | string | 实际到达时间
     *
     * sendStationId array 始发地数据
     *   branchName | string | 站点名称
     *   address    | string | 站点地址
     *   mobile     | string | 手机号
     *
     * receiveStationId array 始发地数据
     *   branchName | string | 站点名称
     *   address    | string | 站点地址
     *   mobile     | string | 手机号
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function packingListDetails()
    {
        $str_box_order_number = $this->_getContentParams('boxOrderNumber'); // 装箱编号
        _toCompare(empty($str_box_order_number), '===', true, 30009);

        $arr_data = ModelStationBoxOrder::getInstance()->toFind(['box_order_number' => $str_box_order_number], 'id AS stationBoxOrderId,station_id AS stationId,box_order_number AS boxOrderNumber,gmt_create AS gmtCreate,receive_station_name AS receiveStationName,state,total_weight AS totalWeight,volume,send_station_name AS sendStationName,send_station_id AS sendStationId,receive_station_id AS receiveStationId,pick_code AS pickCode,box_content AS boxContent,expected_arrival_time AS expectedArrivalTime,actual_arrival_time AS actualArrivalTime');
        $arr_data['operator'] = (string)ModelStationBoxOrderLog::getInstance()->toValue(['box_order_number' => $str_box_order_number], 'operator');
        $int_driver_id = (int)ModelDriverOrder::getInstance()->toValue(['box_id' => $arr_data['stationBoxOrderId']], 'driver_id');
        $arr_data['carNumber'] = (string)ModelDriverInfo::getInstance()->toValue(['id' => $int_driver_id], 'car_number');
        $arr_data['boxContent'] = (int)count(explode(',', $arr_data['boxContent']));
        $arr_data['sendStationId'] = (array)ModelStationInfo::getInstance()->toFind(['id' => $arr_data['sendStationId']], 'branch_name AS branchName,address,mobile');
        $arr_data['sendStationId']['address'] = (string)$this->packingListDetailsAddress($arr_data['sendStationId']['address']);
        $arr_data['receiveStationId'] = (array)ModelStationInfo::getInstance()->toFind(['id' => $arr_data['receiveStationId']], 'branch_name AS branchName,address,mobile');
        $arr_data['receiveStationId']['address'] = (string)$this->packingListDetailsAddress($arr_data['receiveStationId']['address']);
        $int_express_id = (int)ModelStationInfo::getInstance()->toValue(['id' => $arr_data['stationId']], 'express_id');
        $arr_data['expressName'] = (string)ModelExpress::getInstance()->toValue(['express_id' => $int_express_id], 'express_name');

        _returnData(['data' => $arr_data]);
    }

    /**
     * 快件管理列表-查看-备注列表 \n
     * URI : /stationpc/packing.operation.list
     * @param :
     *  name         | type   | null | description
     * --------------|--------|------|-------------
     *  boxOrderNumber        | string  | 必填 | 装箱编号
     *
     * @return
     *  name    | type | description
     * ---------|------|-------------
     *  operator        | string | 操作者
     *  operatorMobile  | string | 操作者账号
     *  mobile          | string | 账号
     *  gmtCreate       | string | 操作时间
     *  state           | int    | 订单状态:10：装箱；15:拆箱；20:待中转；30中转中
     *  remark          | string | 备注
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function packingOperationList(){
        $str_box_order_number = $this->_getContentParams('boxOrderNumber'); // 装箱编号
        _toCompare(empty($str_box_order_number), '===', true, 30009);
        $station_box_order_id = ModelStationBoxOrder::getInstance()->toValue(['box_order_number' => $str_box_order_number], 'id');

        $arr_list = ModelStationBoxOrderLog::getInstance()->toSelect(['box_order_id' => $station_box_order_id], 'operator,state,gmt_create AS gmtCreate,remark,mobile');
        _returnData(['list' => $arr_list]);
    }

    /**
     * 快件管理列表-查看-新增备注 \n
     * URI : /stationpc/packing.add.remark
     * @param :
     *  name         | type   | null | description
     * --------------|--------|------|-------------
     *  boxOrderNumber | int  | 必填 | 装箱编号
     *  remark       | string | 必填 | 备注
     *
     * @return
     *  name    | type | description
     * ---------|------|-------------
     *  ------- | ---- | 无
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function packingAddRemark(){
        $str_box_order_number = $this->_getContentParams('boxOrderNumber'); // 装箱编号
        _toCompare(empty($str_box_order_number), '===', true, 30009);
        $str_remark = $this->_getContentParams('remark'); //备注
        _toCompare(empty($str_remark), '===', true, 30007);
        ModelStationBoxOrderLog::getInstance()->StationOrderLogAdd($str_box_order_number, $str_remark);
        _returnData();
    }

}
